#include "common.h"
#include "ui_common.h"

Common::Common(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Common)
{
    ui->setupUi(this);
    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    connect(ui->pushButton, &QPushButton::clicked, this, [=](){emit playAll(pageType);});
    connect(ui->pageMusicList, &QListWidget::doubleClicked, this, [=](const
   QModelIndex &index){
    // ⿏标双击后，发射信号告诉QQMusic，博能放this⻚⾯中共被双击的歌曲
    emit playMusicByIndex(this, index.row());
        });
}


Common::~Common()
{
    delete ui;
}

void Common::setCommonPageUI(const QString &title, const QString &image)
{
    // 设置标题
    ui->pageTittle->setText(title);
    // 设置封⾯栏
    ui->musicImageLabel->setPixmap(QPixmap(image));
    ui->musicImageLabel->setScaledContents(true);

}

void Common::setMusicListType(PageType pageType)
{
    this->pageType = pageType;
}

void Common::addMusicToMusicPage(MusicList &musicList)
{
    // 将旧内容清空
    musicListOfPage.clear();
    for(auto& music : musicList)
    {
        switch(pageType)
        {
            case LOCAL_PAGE:
                 musicListOfPage.push_back(music.getMusicId());
            break;
            case  LIKE_PAGE:
            {
                if(music.getIsLike())
                {
                 musicListOfPage.push_back(music.getMusicId());
                }
                break;
            }
            case HISTORY_PAGE:
            {
                if(music.getIsHistory())
                {
                    musicListOfPage.push_back(music.getMusicId());
                    break;
                }
            }
            default:
            break;
        }
    }
}

void Common::reFresh(MusicList &musicList)
{
    // 从musicList中分离出当前⻚⾯的所有⾳乐
    ui->pageMusicList->clear();
   addMusicToMusicPage(musicList);
    // 遍历歌单，将歌单中的歌曲显⽰到界⾯
    for(auto musicId : musicListOfPage)
    {
        auto it = musicList.findMusicById(musicId);
        if(it == musicList.end())
        continue;
        ListItemBox* listItemBox = new ListItemBox(ui->pageMusicList);
        listItemBox->setMusicName(it->getMusicName());
        listItemBox->setSinger(it->getSingerName());
        listItemBox->setAlbumName(it->getAlbumName());
        listItemBox->setLikeIcon(it->getIsLike());
        QListWidgetItem* listWidgetItem = new QListWidgetItem(ui->pageMusicList);
        listWidgetItem->setSizeHint(QSize(ui->pageMusicList->width(), 45));
        ui->pageMusicList->setItemWidget(listWidgetItem, listItemBox);
        connect(listItemBox, &ListItemBox::setIsLike, this, [=](bool isLike){
        emit updateLikeMusic(isLike, it->getMusicId());
        });
    }
    // 更新完成后刷新下界⾯
    ui->pageMusicList->repaint();
}

void Common::addMusicToPlayer(MusicList &musicList, QMediaPlaylist *playList)
{
    // 根据⾳乐列表中⾳乐所属的⻚⾯，将⾳乐添加到playList中
    for(auto music : musicList)
    {
        switch(pageType)
        {
            case LOCAL_PAGE:
            {
                playList->addMedia(music.getMusicUrl());
                break;
            }
            case LIKE_PAGE:
            {
                if(music.getIsLike())
                {
                    playList->addMedia(music.getMusicUrl());
                }
                    break;
            }
            case HISTORY_PAGE:
            {
                if(music.getIsHistory())
                {
                    playList->addMedia(music.getMusicUrl());
                }
                break;
            }
            default:
            break;
        }
    }
}

const QString &Common::getMusicIdByIndex(int index) const
{
    if(index >= musicListOfPage.size())
    {
    qDebug()<<"⽆此歌曲";
    return "";
    }
    return musicListOfPage[index];
}

void Common::setImageLabel(QPixmap pixMap)
{
    ui->musicImageLabel->setPixmap(pixMap);
    ui->musicImageLabel->setScaledContents(true);
}

